﻿using System;
using System.Web.Mvc;

using Devbridge.IPolice.ServiceContracts;
using Devbridge.IPolice.Services;
using Devbridge.IPolice.Web.Models.Crimes;

namespace Devbridge.IPolice.Web.Controllers
{
    public class CrimesController : Controller
    {
        private ICrimeService crimeService;

        public CrimesController()
        {
            crimeService = new CrimeService();
        }

        public ActionResult List(string filterByName)
        {
            int count;
            var crimesData = crimeService.GetSmallCrimes(out count);
            var lastCrime = crimeService.GetLastCrime();

            CrimeListViewModel model = new CrimeListViewModel();
            model.CrimesCount = count;
            model.LastCrime = new CrimeViewModel
                                  {
                                      Description = lastCrime.Description,
                                      Date = lastCrime.CrimeDate
                                  };

            foreach (var crime in crimesData)
            {
                model.Crimes.Add(new CrimeViewModel
                                       {
                                           Description = crime.Description,
                                           Date = crime.CrimeDate
                                       });
            }            
            
            return View(model);
        }

        protected override void Dispose(bool disposing)
        {
            crimeService.Dispose();
            base.Dispose(disposing);
        }
    }
}
